package top.zk123.chain.wechat;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import top.zk123.chain.bean.ReturnInfo;
import top.zk123.chain.exception.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 微信小程序 接口层
 *
 * @author Ke Zhang
 * @since 2.1
 */
@RestController
public class WechatController {

    @Resource
    WechatService wechatService;

    /**
     * 微信登录操作
     *
     * @param code 临时登录凭证
     * @return ReturnInfo
     */
    @GetMapping("/wx/login")
    public ReturnInfo wxLogin(@RequestParam(value = "code") String code) {
        return wechatService.wxLogin(code);
    }


    /**
     * 更新用户信息
     *
     * @param company   公司
     * @param education 学历
     * @param school    毕业于
     * @return ReturnInfo
     */
    @PostMapping("/wx/update")
    public ReturnInfo wxUpdate(@RequestParam(value = "company") String company,
                               @RequestParam(value = "education") String education,
                               @RequestParam(value = "school") String school) {
        return wechatService.wxUpdate(company, education, school);
    }

    /**
     * 查询message列表
     *
     * @return ReturnInfo
     */
    @GetMapping("/wx/msg/list")
    public ReturnInfo wxMessage() {
        return wechatService.wxMessage();
    }


    /**
     * 更改证书授权状态
     *
     * @param certUId 证书 UID
     * @return ReturnInfo
     */
    @PostMapping("/wx/msg/change")
    public ReturnInfo wxMessageStateChange(@RequestParam(value = "certUId") String certUId) {
        return wechatService.wxMessageStateChange(certUId);
    }

    /**
     * 返回证书列表, 缩略版, 仅2条记录
     *
     * @return ReturnInfo
     */
    @GetMapping("/wx/cert/list")
    public ReturnInfo wxGet2Certs() {
        return wechatService.wxGetCertList(false);
    }

    /**
     * 返回证书列表,缩略版, 全部记录
     *
     * @return returnInfo
     */
    @GetMapping("/wx/cert/list/all")
    public ReturnInfo wxGetAllCerts() {
        return wechatService.wxGetCertList(true);
    }

    /**
     * 查询单张证书的详细信息
     *
     * @param certUIdList 证书 UId 列表
     * @return ReturnInfo
     */
    @GetMapping("/wx/certs")
    public ReturnInfo wxGetCertDetail(@RequestParam(value = "UIdList") List<Long> certUIdList) throws
            WXSystemBusyException {
        return wechatService.wxGetCertsByUId(certUIdList);
    }

    // 微信端 返回用户已失效的证书列表
    @GetMapping("/wx/invalidCert/list")
    public ReturnInfo wxGetInvalidCerts() throws NoAuthenticationException, CertificateNotFoundException {
        return wechatService.wxGetInvalidCerts();
    }

    /**
     * 实名制核验
     *
     * @param cardNo   身份证号
     * @param realName 真实姓名
     * @param face     身份证人脸面图片  建议小于 1.5MB
     * @param back     身份证背面图片   建议小于 1.5MB
     * @return ReturnInfo
     * @throws FileUploadException        文件上传失败
     * @throws WrongFileTypeException     不支持的文件类型
     * @throws FileEmptyException         文件为空
     * @throws EmptyStringException       缺少后缀名
     * @throws FakeIdCardException        假的身份证
     * @throws UserVerifyFailed           核验失败
     * @throws IdCardExpiredException     身份证过期
     * @throws OCRInputDifferentException OCR识别与输入数据不符
     */
    @GetMapping("/wx/verify/fake")
    public ReturnInfo wxUserVerify(@RequestParam(value = "cardNo") String cardNo,
                                   @RequestParam(value = "realName") String realName,
                                   @RequestParam(value = "face") String face,
                                   @RequestParam(value = "back") String back) throws
            FileUploadException, WrongFileTypeException, FileEmptyException, EmptyStringException,
            FakeIdCardException, UserVerifyFailed, IdCardExpiredException, OCRInputDifferentException {
        return wechatService.wxUserVerify(cardNo, realName, face, back);
    }

    /**
     * 人证核验, 图片通过网址形式传入
     *
     * @param cardNo   身份证号
     * @param realName 真实姓名
     * @param face     String 链接
     * @param back     String 链接
     * @return ReturnInfo
     * @throws IdCardExpiredException     证件过期
     * @throws OCRInputDifferentException 输入值与 OCR 结果不同
     * @throws UserVerifyFailed           用户验证失败
     * @throws FakeIdCardException        假证
     */
    @GetMapping("/wx/verifyWithLink")
    public ReturnInfo wxUserVerifyWithLink(@RequestParam(value = "cardNo") String cardNo,
                                           @RequestParam(value = "realName") String realName,
                                           @RequestParam(value = "face") String face,
                                           @RequestParam(value = "back") String back) throws
            IdCardExpiredException, OCRInputDifferentException, UserVerifyFailed, FakeIdCardException {
        return wechatService.wxUserVerifyWithLink(cardNo, realName, face, back);
    }

    /**
     * 微信临时联调接口
     *
     * @param cardNo   身份证号
     * @param realName 真实姓名
     * @param face     String 链接
     * @param back     String 链接
     * @return ReturnInfo
     */
    @GetMapping("/wx/verify")
    public ReturnInfo wxUserVerifyFake(@RequestParam(value = "cardNo") String cardNo,
                                       @RequestParam(value = "realName") String realName,
                                       @RequestParam(value = "face") String face,
                                       @RequestParam(value = "back") String back) {
        return wechatService.wxUserVerifyFake(cardNo, realName, face, back);
    }

    /**
     * 微信端导出数字证书
     *
     * @param request  请求
     * @param response 响应
     */
    @GetMapping("/wx/exportJSON")
    public void wxExportJSON(HttpServletRequest request, HttpServletResponse response,
                             @RequestParam(value = "UId") long UId) {
        wechatService.wxExportJSON(request, response, UId);
    }

    /**
     * 微信端获取 activityCode
     *
     * @return ReturnInfo
     * @throws InvalidAccessTokenException Token 无效
     * @throws WXSystemBusyException       微信系统忙
     */
    @GetMapping("/wx/share")
    public ReturnInfo share() throws InvalidAccessTokenException, WXSystemBusyException {
        return wechatService.wxShare();
    }

    /**
     * 微信获取小程序码
     *
     * @param path 小程序路径
     * @return ReturnInfo
     */
    @GetMapping("/wx/acode")
    public ReturnInfo wxGetApplicationCode(@RequestParam(value = "path") String path) {
        return wechatService.wxApplicationCodeExport(path);
    }

    @PostMapping(value = "/wx/uploadImg")
    public ReturnInfo wxUploadUserPic(@RequestParam(value = "img") MultipartFile file) throws FileEmptyException, WrongFileTypeException, FileUploadException {
        return wechatService.wxUploadUserPic(file);
    }
}
